package com.example.ll_video.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ll_video.bean.SubjectType;
import com.example.ll_video.R;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SubjectTypeAdapter extends RecyclerView.Adapter<SubjectTypeAdapter.ViewHolder> {


    private Context context;
    private ItemClickListener itemClickListener;
    private List<SubjectType> myData;
    private int selectedPosition = 0;

    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;
    }

    public SubjectTypeAdapter(Context context, List<SubjectType> myData) {
        this.context = context;
        this.myData = myData;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.subject_type_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        SubjectType subjectType = myData.get(position);
        holder.tvTypeName.setText(subjectType.getItemName());
        if (subjectType.isSelected()) {
            holder.llSelectedLine.setVisibility(View.VISIBLE);
            holder.tvTypeName.setTextColor(context.getResources().getColor(R.color.blue));
        } else {
            holder.llSelectedLine.setVisibility(View.INVISIBLE);
            holder.tvTypeName.setTextColor(context.getResources().getColor(R.color.text_color));
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                itemClickListener.onItemClick(position,subjectType);
            }
        });
    }

    @Override
    public int getItemCount() {
        return myData.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.tv_type_name)
        TextView tvTypeName;
        @BindView(R.id.ll_selected_line)
        LinearLayout llSelectedLine;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void refreshItem(int position) {
        SubjectType subjectType1 = myData.get(selectedPosition);
        subjectType1.setSelected(false);
        notifyItemChanged(selectedPosition, subjectType1);

        SubjectType subjectType2 = myData.get(position);
        subjectType2.setSelected(true);
        notifyItemChanged(position, subjectType2);
        selectedPosition = position;
    }
    public interface ItemClickListener{
        void onItemClick(int position,SubjectType subjectType);
    }

}
